package com.example.zhaoping.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.zhaoping.entity.*;
import com.example.zhaoping.serviceImpl.EnterpriseServiceImpl;
import com.example.zhaoping.serviceImpl.RecruitmentServiceImpl;
import com.example.zhaoping.util.Result;
import com.example.zhaoping.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.http.HttpRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("enterprise")
public class EnterpriseController {
    @Autowired
    private Result result;
    @Resource
    private EnterpriseServiceImpl enterpriseService;
    @Resource
    private RecruitmentServiceImpl recruitmentService;

    @GetMapping(value = "/get_enterprise",name = "获取企业")
    public Result getEnterprise(@RequestHeader("Authorization") String token){
        int id = TokenUtil.getUserId(token);
        Enterprise enterprise = enterpriseService.selectById(id);
        if(enterprise != null){
            return result.success(enterprise);
        }else {
            return result.error("获取企业信息失败！");
        }
    }
    @GetMapping(value = "/get_job",name = "获取岗位")
    public Result getJob(@RequestHeader("Authorization") String token, PageParam param){
        int id = TokenUtil.getUserId(token);
        Page page = recruitmentService.getJob(id,param);
        return result.success(page);
    }
    @GetMapping(value = "getJobById",name = "根据id获取职位")
    public Result getJobById(int id){
        Recruitment recruitment = recruitmentService.getJobById(id);
        if(recruitment!=null){
            return result.success(recruitment);
        }else{
            return result.error("获取职位失败！");
        }
    }
    @PostMapping(value = "/del_job",name = "删除岗位")
    public Result delJob(Integer[] idList){
        List<Integer> list = Arrays.stream(idList).collect(Collectors.toList());
        int isDel = recruitmentService.delJob(list);
        if(isDel!=0){
            return result.success();
        }else{
            return result.error("删除失败！");
        }
    }
    @PostMapping(value = "/add_job",name = "添加岗位")
    public Result addJob(Recruitment recruitment){
        recruitment.setReleaseTime(new Date());
        int addJob = recruitmentService.addJob(recruitment);
        if(addJob!=0){
            return result.success("添加成功！");
        }else{
            return result.error("添加失败！");
        }
    }
    @PostMapping(value = "/edit_job",name = "修改岗位")
    public Result editJob(Recruitment recruitment){
        recruitment.setReleaseTime(new Date());
        int editJob = recruitmentService.editJob(recruitment);
        if(editJob!=0){
            return result.success();
        }else{
            return result.error("修改失败！");
        }

    }

}
